/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */
package org.apache.directory.studio.proxy.handlers;


import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.message.BindRequest;
import org.apache.directory.ldap.client.api.message.BindResponse;
import org.apache.directory.ldap.client.api.message.LdapResult;
import org.apache.directory.shared.ldap.message.BindResponseImpl;
import org.apache.directory.shared.ldap.message.internal.InternalBindRequest;
import org.apache.directory.shared.ldap.message.internal.InternalBindResponse;
import org.apache.directory.shared.ldap.name.DN;
import org.apache.directory.studio.proxy.LdapConnectionStore;
import org.apache.directory.studio.proxy.ProxyIO;
import org.apache.mina.core.session.IoSession;
import org.dom4j.Document;


/**
 * Request handler for a bind request.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class BindHandler extends AbstractMessageHandler<InternalBindRequest>
{
    /* (non-Javadoc)
     * @see org.apache.directory.studio.proxy.handlers.AbstractMessageHandler#handleMessage(org.apache.mina.core.session.IoSession, java.lang.Object)
     */
    public void handleMessage( IoSession session, InternalBindRequest message ) throws Exception
    {
        super.handleMessage( session, message );

        //Get file/document for session
        Document document = ProxyIO.getDocumentForSession( session );
        //Serializing Bind request        

        ProxyIO.writeBindRequest( message, document, session );

        // Creating the LDAP Connection
        LdapConnection connection = new LdapConnection( "localhost", 10389 ); // TODO: Change these parameters to something more generic

        // Storing the connection in the LDAP Connection Store
        LdapConnectionStore.setConnection( session, connection );

        // Forwarding the Bind Request
        BindRequest bindRequest = new BindRequest();
        bindRequest.setMessageId( message.getMessageId() );
        bindRequest.setName( message.getName().getName() );
        bindRequest.setCredentials( message.getCredentials() );
        BindResponse bindResponse = connection.bind( bindRequest );

        // This is an example...
        // We are always responding successfully to a bind request

        // Creating the response with result code set to 'success'
        InternalBindResponse response = new BindResponseImpl( bindResponse.getMessageId() );
        LdapResult result = bindResponse.getLdapResult();
        response.getLdapResult().setErrorMessage( result.getErrorMessage() );
        response.getLdapResult().setMatchedDn( new DN( result.getMatchedDn() ) );
        response.getLdapResult().setResultCode( result.getResultCode() );

        //Serializing Bind response
        ProxyIO.writeBindResponse( response, document, session );

        // Writing the response
        writeResponse( response.toString() );
        session.write( response );
    }

}
